#include<bits/stdc++.h>
using namespace std;
const int N = 100010;
const __int128 one = 1;
__int128 f(__int128 a,__int128 b,__int128 c,__int128 n){
    //cerr<<a<<' '<<b<<' '<<c<<' '<<n<<'\n';
    if(n <= 0){
        return 0;
    }
    if(a == 0){
        return (b/c)*n;
    }
    if((a*n + b)/c < 1){
        return 0;
    }
    if(max(a,b) >= c){
        return f(a % c,b % c,c,n) + (a/c)*n*(n+1)/2 + (b/c)*n;
    }else{
        return n*((a*n + b)/c) - f(c,c - b - 1,a,(a*n + b)/c - 1) - (c - b - 1)/a;
    }
}
struct segment_tree{
    int pcnt;
    __int128 sum[N*50];
    bool stag[N*50];
    int sltag[N*50],satag[N*50],sbtag[N*50];
    int ls[N*50],rs[N*50];
    void set_val(int &now,int L,int a,int b,int l,int r){
        if(!now){
            now = ++pcnt;
        }
        stag[now] = 1;
        sltag[now] = L;
        satag[now] = a;
        sbtag[now] = b;
        sum[now] = one*(r - l + 1)*(one*(l - L + 1)*a + one*(r - L + 1)*a)/2 - f(a,one*(l - L)*a,b,r - l + 1)*b;
    }
    void push_down(int now,int l,int r){
        if(stag[now]){
            int mid = (l + r)/2;
            set_val(ls[now],sltag[now],satag[now],sbtag[now],l,mid);
            set_val(rs[now],sltag[now],satag[now],sbtag[now],mid+1,r);
            stag[now] = 0;
        }
    }
    void update(int &now,int l,int r,int x,int y,int a,int b){
        if(!now){
            now = ++pcnt;
        }
        if(x <= l && r <= y){
            set_val(now,x,a,b,l,r);
            return;    
        }
        push_down(now,l,r);
        int mid = (l + r)/2;
        if(x <= mid){
            update(ls[now],l,mid,x,y,a,b);
        }
        if(mid < y){
            update(rs[now],mid+1,r,x,y,a,b);
        }
        sum[now] = sum[ls[now]] + sum[rs[now]];
    }
    __int128 query(int now,int l,int r,int x,int y){
        if(x <= l && r <= y){
            return sum[now];
        }
        int mid = (l + r)/2;
        __int128 su = 0;
        push_down(now,l,r);
        if(x <= mid){
            su += query(ls[now],l,mid,x,y);
        }
        if(mid < y){
            su += query(rs[now],mid+1,r,x,y);
        }
        return su;
    }
}sg;
int main(){
    int n,q;
    cin>>n>>q;
    int rot = 0;
    for(int i = 1;i<=q;i++){
        int op;
        cin>>op;
        if(op == 1){
            int l,r,a,b;
            cin>>l>>r>>a>>b;
            sg.update(rot,1,n,l,r,a,b);  
        }else{
            int l,r;
            cin>>l>>r;
            cout<<((long long )sg.query(rot,1,n,l,r))<<'\n';
        }
    }
}